package cn.scw.project.facility.service.impl;

import cn.scw.common.constant.DictTypeConsts;
import cn.scw.common.constant.FacConsts;
import cn.scw.common.exception.CustomException;
import cn.scw.common.utils.SecurityUtils;
import cn.scw.common.utils.StringUtils;
import cn.scw.common.utils.bean.ToolUtils;
import cn.scw.common.utils.sql.JpaUtil;
import cn.scw.framework.security.LoginUser;
import cn.scw.project.facility.domain.FacFacility;
import cn.scw.project.facility.domain.FacFacilityRepairs;
import cn.scw.project.facility.domain.netework.bean.FacFacilityRepairsAddReq;
import cn.scw.project.facility.domain.netework.bean.FacFacilityRepairsListReq;
import cn.scw.project.facility.domain.netework.bean.FacFacilityRepairsUpdReq;
import cn.scw.project.facility.mapper.FacFacilityRepairsMapper;
import cn.scw.project.facility.service.IFacFacilityRepairsService;
import cn.scw.project.facility.service.IFacFacilityService;
import cn.scw.project.system.domain.SysDictData;
import cn.scw.project.system.domain.SysUser;
import cn.scw.project.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 设备报修Service业务层处理
 *
 * @author api
 * @date 2020-05-20
 */
@Service
public class FacFacilityRepairsServiceImpl implements IFacFacilityRepairsService {
    @Autowired
    private FacFacilityRepairsMapper facFacilityRepairsMapper;
    @Autowired
    private IFacFacilityService iFacFacilityService;
    @Autowired
    private ISysDictDataService iSysDictDataService;

    /**
     * 查询设备报修
     *
     * @param id 设备报修ID
     * @return 设备报修
     */
    @Override
    public FacFacilityRepairs selectFacFacilityRepairsById(Long id) {
        return facFacilityRepairsMapper.selectFacFacilityRepairsById(id);
    }

    /**
     * 查询设备报修列表
     *
     * @param req 设备报修
     * @return 设备报修
     */
    @Override
    public List<FacFacilityRepairs> selectFacFacilityRepairsList(FacFacilityRepairsListReq req) {
        req.setFacilityCode(JpaUtil.getNullOrLikeStr(req.getFacilityCode()));
        req.setUserPhone(JpaUtil.getNullOrLikeStr(req.getUserPhone()));
        return facFacilityRepairsMapper.selectFacFacilityRepairsList(req);
    }

    @Override
    public List<FacFacilityRepairs> selectFacFacilityRepairsListByUserId(Boolean isSolve) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        return facFacilityRepairsMapper.selectFacFacilityRepairsListByUserId(user.getUserId(), isSolve);
    }

    /**
     * 新增设备报修
     *
     * @param req 设备报修
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertFacFacilityRepairs(FacFacilityRepairsAddReq req) {
        if (StringUtils.isBlank(req.getFacilityCode()))
            throw new CustomException("请选择设备");
        if (!iFacFacilityService.existsByCode(req.getFacilityCode()))
            throw new CustomException("设备不存在");
//        if (StringUtils.isBlank(req.getUserPhone()))
//            throw new CustomException("请输入电话");
//        if (!RegexUtils.isPhone(req.getUserPhone()))
//            throw new CustomException("请输入正确的电话");
        if (!iSysDictDataService.existsDictLabel(DictTypeConsts.FAC_REPAIRS_FAULT_TYPE, req.getFaultTypeCode()))
            throw new CustomException("请选择正确的工单类型");
        FacFacilityRepairs facFacilityRepairs = new FacFacilityRepairs();
        ToolUtils.copyPropertiesIgnoreNull(req, facFacilityRepairs);
        LoginUser loginUser=SecurityUtils.getLoginUser();
        if (loginUser!=null) {
            SysUser user = loginUser.getUser();
            if (user!=null)
                facFacilityRepairs.setUserId(user.getUserId());
        }

        FacFacility facFacility = iFacFacilityService.selectFacFacilityByCode(req.getFacilityCode());
        facFacility.setStatus(FacConsts.FACILITY_STATUS_REPAIR);
        iFacFacilityService.updateFacFacility(facFacility);

        return facFacilityRepairsMapper.insertFacFacilityRepairs(facFacilityRepairs);
    }

    /**
     * 修改设备报修
     *
     * @param req 设备报修
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateFacFacilityRepairs(FacFacilityRepairsUpdReq req) {
        FacFacilityRepairs facFacilityRepairs = this.selectFacFacilityRepairsById(req.getId());
        if (facFacilityRepairs == null)
            throw new CustomException("设备报修记录不存在");
        if (req.getIsSolve() != null) {
            facFacilityRepairs.setSolve(req.getIsSolve());
            FacFacility facFacility = iFacFacilityService.selectFacFacilityByCode(facFacilityRepairs.getFacilityCode());
            if (facFacility != null) {
                if (req.getIsSolve()) {
                    facFacility.setStatus(FacConsts.FACILITY_STATUS_NORMAL);
                } else {
                    facFacility.setStatus(FacConsts.FACILITY_STATUS_REPAIR);
                }
                iFacFacilityService.updateFacFacility(facFacility);
            }
        }
        return facFacilityRepairsMapper.updateFacFacilityRepairs(facFacilityRepairs);
    }

    /**
     * 批量删除设备报修
     *
     * @param ids 需要删除的设备报修ID
     * @return 结果
     */
    @Override
    public int deleteFacFacilityRepairsByIds(Long[] ids) {
        return facFacilityRepairsMapper.deleteFacFacilityRepairsByIds(ids);
    }

    /**
     * 删除设备报修信息
     *
     * @param id 设备报修ID
     * @return 结果
     */
    @Override
    public int deleteFacFacilityRepairsById(Long id) {
        return facFacilityRepairsMapper.deleteFacFacilityRepairsById(id);
    }

    @Override
    public List<SysDictData> selectFacFacilityRepairsTypeList() {
        return iSysDictDataService.selectDictDataByType(DictTypeConsts.FAC_REPAIRS_FAULT_TYPE);
    }
}
